#include<bits/stdc++.h>
using namespace std;

long long gcd(long long a, long long b){
	if(a % b == 0){
		return b;
	}else{
		return gcd(b, a%b);
	}
}

long long ex_gcd(long long a,long long b,long long &x,long long &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    long long gcd=ex_gcd(b,a%b,x,y);
    long long temp=x;
    x=y;
    y=temp-a/b*y;
    return gcd;
}

int main(){
	int t;
	long long a, b;
	scanf("%d", &t);
	while(t--){
		scanf("%lld %lld", &a, &b);
		a = 180*b-2*a;
		long long gcdx = gcd(a, b);
		a /= gcdx;
		b /= gcdx;
		long long ans = gcd(a, 360*b);
		printf("%lld\n", 360*b/ans-1);
	}
	return 0;
}
